﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;
using Hartley.Util;
namespace WebApplication1
{
    public partial class WviewAthleticDataYrEvent : System.Web.UI.Page
    {
        // Rows property to hold the Rows in the ViewState
        protected int Rows
        {
            get
            {
                return ViewState["Rows"] != null ? (int)ViewState["Rows"] : 0;
            }
            set
            {
                ViewState["Rows"] = value;
            }
        }

        // Columns property to hold the Columns in the ViewState
        protected int Columns
        {
            get
            {
                return ViewState["Columns"] != null ? (int)ViewState["Columns"] : 0;
            }
            set
            {
                ViewState["Columns"] = value;
            }
        }

        public List<AthleticDataDTO> AllAthleticData
        {
            get
            {
                if (Session["AllAthleticData"] == null)
                {
                    return athleticAL.GetAllAthleticData();
                }
                else
                {
                    return (List<AthleticDataDTO>)Session["AllAthleticData"];
                }
            }
            set
            {
                Session["AllAthleticData"] = value;
            }
        }

        AthleticAL athleticAL;
        SportsUtil sportsUtil;
        private const int EVENT_PER_ROW = 2;


        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                CreateDynamicTable();

            }
        }

        private void CreateDynamicTable()
        {
            PlaceHolder4.Controls.Clear();

            // Fetch the number of Rows and Columns for the table 
            // using the properties
            sportsUtil = new SportsUtil();
            athleticAL = new AthleticAL();

            List<string> allEvents = new List<string>();
            List<AthleticDataDTO> allAthleticData = new List<AthleticDataDTO>();
            allAthleticData = athleticAL.GetAllAthleticData();
            List<AthleticEventDTO> allAthleticDataEvent = athleticAL.GetAllAgeDivisionAndEventCategoryFromAthleticDataList(allAthleticData);
            string eventCategory = string.Empty;
            foreach (AthleticEventDTO athleticData in allAthleticDataEvent)
            {
                eventCategory = athleticData.EventCategory;
                allEvents.Add(eventCategory);
            }
            allEvents = allEvents.Distinct().ToList();


            int tblRows = allEvents.Count / EVENT_PER_ROW;
            int tblCols = 0;

            // Create a Table and set its properties 
            Table tbl = new Table();
            tbl.Width = new Unit("100%");
            tbl.BorderStyle = BorderStyle.Solid;
            // Add the table to the placeholder control
            PlaceHolder4.Controls.Add(tbl);
            // Now iterate through the table and add your controls 



            for (int i = 0; i <= tblRows; i++)
            {
                TableRow tr = new TableRow();
                tr.Width = new Unit("50%");



                if (i != tblRows && tblRows >= 0)
                {
                    tblCols = EVENT_PER_ROW;
                }
                else
                {
                    tblCols = allEvents.Count % EVENT_PER_ROW;
                }


                for (int j = 0; j < tblCols; j++)
                {
                    TableCell tc = new TableCell();

                    HyperLink hlCompetitionDivisionCell = new HyperLink();
                    hlCompetitionDivisionCell.Text = allEvents[i * EVENT_PER_ROW + j].ToString();
                    hlCompetitionDivisionCell.NavigateUrl = "viewAthleticDataAsGrid.aspx?" + allEvents[i * EVENT_PER_ROW + j].ToString();

                    if (i % 2 == 0)
                    {
                        tc.Style.Add("background-color", "PURPLE");
                        tc.ForeColor = System.Drawing.Color.White;
                    }
                    else
                    {
                        tc.Style.Add("background-color", "White");
                        tc.ForeColor = System.Drawing.Color.Purple;
                    }



                    // Add the control to the TableCell
                    tc.Controls.Add(hlCompetitionDivisionCell);
                    // Add the TableCell to the TableRow
                    tr.Cells.Add(tc);
                }

                // Add the TableRow to the Table
                tbl.Rows.Add(tr);
            }

            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not

            ViewState["dynamictable"] = true;
        }
    }
}